package com.yuan.yblog.portal.pojo.VO;

import com.yuan.yblog.crud.entity.Post;
import com.yuan.yblog.portal.pojo.contants.PostConstant;
import com.yuan.yblog.portal.pojo.contants.Status;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "文章提交传输对象")
public class PostVO extends Post {

    /**
     * 转化为用于更新的实体
     */
    public Post toUpdateEntity(){
        Post post = new Post();
        //配置提交信息
        post.setId(this.getId());
        post.setTitle(this.getTitle());
        post.setContent(this.getContent());
        post.setEditMode(this.getEditMode());
        post.setModified(this.getModified());
        return post;
    }

    /**
     * 转化为用于插入的实体
     */
    public Post toInsertEntity() {
        Post post = new Post();
        //配置表单数据
        post.setId(null);
        post.setTitle(this.getTitle());
        post.setContent(this.getContent());
        post.setEditMode(this.getEditMode());
        post.setCategoryId(this.getCategoryId());
        post.setUserId(this.getUserId());
        //初始化数据
        post.setVoteUp(PostConstant.zero);
        post.setVoteDown(PostConstant.zero);
        post.setViewCount(PostConstant.zero);
        post.setCommentCount(PostConstant.zero);
        post.setRecommend(PostConstant.zero);
        post.setLevel(PostConstant.initLevel);
        post.setStatus(Status.valid);
        post.setCreated(new Date());
        post.setModified(new Date());

        return post;
    }
}
